package com.book.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.db.sql.Order;
import com.book.entity.Account;
import com.book.entity.BookOrder;
import com.book.entity.BookOrderItem;
import com.book.service.AddressService;
import com.book.service.OrderService;
import com.book.util.ResultBack;
import com.book.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Resource
    private Snowflake snowflake;
    @Resource
    private AddressService addressService;

    @Resource
    private OrderService orderService;


    @RequestMapping("/toOrder")
    public String toOrder(int[] cartId, Long[] bookId, int[] buyCount, double[] subTotal, String total, HttpSession httpSession) {

        for (int i = 0; i < bookId.length; i++) {
            log.info(" cartId: " + cartId[i] + " bookId: " + bookId[i] + "  buyCount: " + buyCount[i] + "  subTotal: " + subTotal[i]);
        }
        //1、生成订单
        //生成雪花ID
        long orderId = snowflake.nextId();
        //获取用户ID
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();

        int addressId = addressService.selectDefaultAddressByUserId(userId.intValue());

        //生成订单
        BookOrder bookOrder = new BookOrder(orderId, userId, addressId, new BigDecimal(total), (byte) 0, new Date());

        //2、生成订单详情
        ArrayList<BookOrderItem> bookOrderItems = new ArrayList<>();

        for (int i = 0; i < bookId.length; i++) {
            bookOrderItems.add(new BookOrderItem(orderId, bookId[i], buyCount[i], new BigDecimal(subTotal[i])));
        }

        orderService.addOrder(bookOrder, bookOrderItems, cartId);

        return "redirect:/order/showOrder/" + orderId;
    }

    @RequestMapping("/showOrder/{orderId}")
    public String showOrder(@PathVariable Long orderId, Model model) {

        OrderVo orderVo = orderService.findOrderByOrderId(orderId);

        model.addAttribute("orderVo", orderVo);
        return "order";
    }


    //若中途退出，刷新订单状态,更新数据库信息回显到订单列表中
    @RequestMapping("/resetOrderStatus")
    @ResponseBody
    public ResultBack resetOrderStatus(BookOrder bookOrder) {
        int i = orderService.updateOrderOrderStatus(bookOrder);
        if (i != 0) {
            return ResultBack.success("订单支付成功", i);
        } else {
            return ResultBack.failure("订单支付失败");
        }
    }

    @RequestMapping("/showAllPayOrder")
    public String showAllPayOrder(HttpSession httpSession,Model model) {
        //获取用户ID
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();
        
        List<BookOrder> list = orderService.showAllPayOrder(userId);
        
        model.addAttribute("orderlist",list);
        
        return "show_order";
    }


    @RequestMapping("/showDetailOrder")
    public String showDetailOrder(Long orderId, Model model) {
        
        List<BookOrderItem> list = orderService.showDetailOrder(orderId);

        model.addAttribute("orderitemlist",list);

        return "all_order";
    }

    @RequestMapping("/waitPayOrder")
    @ResponseBody
    public ResultBack waitPayOrder(HttpSession httpSession) {
        //获取用户ID
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();

        List<BookOrder> list = orderService.waitPayOrder(userId);
        
        return ResultBack.success("查询成功",list);
    }

    @RequestMapping("/updatePayStatus")
    @ResponseBody
    public ResultBack updatePayStatus(BookOrder bookOrder) {
        
        int i = orderService.updatePayStatus(bookOrder);

        return ResultBack.success("查询成功",i);
    }
    
    @RequestMapping("/selectWaitOrder")
    public String selectWaitOrder(Byte status,HttpSession httpSession, Model model) {
        //获取用户ID
        Account account = (Account) httpSession.getAttribute("account");
        Long userId = account.getId();

        List<BookOrder> list = orderService.selectWaitOrder(userId,status);

        model.addAttribute("waitorderlist",list);
        
        return "wait_order";
    }
}

